package com.pengpeng.advice.web;

import com.pengpeng.advice.exception.CMSException;
import com.pengpeng.advice.result.ResultCodeEnum;
import com.pengpeng.advice.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 彭鹏
 * @date 2021/10/29.
 */
@Api(value = "test全局处理")
@RestController
public class HelloController {

    @GetMapping({"/test"})
    @ApiOperation(value = "desc of method", httpMethod = "GET")
    public String testException(@ApiParam(value = "desc of param", required = true) @RequestParam("user") String user) {
        if ("1".equals(user)) {
            throw new CMSException(ResultCodeEnum.PARAM_ERROR);
        }
        return "index";
    }

    @GetMapping({"/test2"})
    @ApiOperation(value = "desc of method", httpMethod = "GET")
    public String testException2(@ApiParam(value = "desc of param", required = true) @RequestParam("user") String user) {
        int a = 9 / 0;
        return "index";
    }

    @Autowired
    private UserService userService;

    @PostMapping({"/add"})
    @ApiOperation(value = "desc of method", httpMethod = "POST")
    public String add(@ApiParam(value = "desc of param", required = true) @RequestParam("user") String user) {
        int num = userService.insert(user);
        return "index";
    }
    @PostMapping({"/add2"})
    @ApiOperation(value = "desc of method", httpMethod = "POST")
    public String add2(@ApiParam(value = "desc of param", required = true) @RequestParam("user") String user) {
        int num = userService.insert2(user);
        return "index2";
    }
}

